package com.ctsi.framework.common.convert;

import com.ctsi.commons.util.UtilDateTime;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import org.springframework.boot.jackson.JsonComponent;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

@JsonComponent
public class DateSerializerConvert {

    public static class DateSerializer extends JsonSerializer<Date> {

        @Override
        public void serialize(Date value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            gen.writeString(sf.format(value));
        }
    }

    public static class DateTimeSerializer extends JsonSerializer<Date> {
        @Override
        public void serialize(Date value, JsonGenerator gen, SerializerProvider provider) throws IOException {
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            gen.writeString(sf.format(value));
        }
    }

    public static class Long2DateSerialize extends JsonSerializer<Long> {

        @Override
        public void serialize(Long value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
            String dateStr = UtilDateTime.longToString(value,"yyyy-MM-dd");
            gen.writeString(dateStr);
        }
    }

    public static class Long2DateTimeSerialize extends JsonSerializer<Long> {
        @Override
        public void serialize(Long value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
            String dateStr = UtilDateTime.longToString(value,"yyyy-MM-dd HH:mm:ss");
            gen.writeString(dateStr);
        }
    }


}
